let id = 0;

class Dep {
    constructor() {
        this.id = id++;
        this.subs = []; // 存放 watcher 的容器
    }
    depend() {
        if(Dep.target) {
            Dep.target.addDep(this);
        }
    }
    notify() {
        this.subs.forEach(watcher => watcher.update())
    }
    addSub(watcher) {
        this.subs.push(watcher);
    }
}

Dep.target = null;
const targetStack = [];

export function pushTarget(watcher) {
    targetStack.push(watcher);
    Dep.target = watcher;
}
export function popTarget() {
    targetStack.pop();
    Dep.target = targetStack[targetStack.length - 1]
}
export default Dep;